using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.IO;
public partial class ViewCCR : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HiddenFieldSortOrder.Value = "ASC";
            HiddenFieldSortExpression.Value = "Creation_Date_Time";
            BindGridView();
        }
    }

    private void BindGridView()
    {

        if (!string.IsNullOrEmpty(ControlHelperClass.CCRFolderPath))
        {
            DirectoryInfo dirInfo = new DirectoryInfo(ControlHelperClass.CCRFolderPath);
            FileInfo[] fileInfo = dirInfo.GetFiles("*.xml", SearchOption.AllDirectories);
            DataTable CCRTable = new DataTable();
            CCRTable.Columns.Add("Creation_Date_Time");
            CCRTable.Columns.Add("CCR_Document");
            CCRTable.Columns.Add("Upload");
            foreach (FileInfo finfo in fileInfo)
            {
                if (finfo.Name.Contains(ControlHelperClass.PatientId.ToString()))
                {
                    object[] parameter = new object[3];
                    parameter[0] = finfo.CreationTime;
                    parameter[1] = finfo.Name;
                    if (finfo.FullName.ToLower().Contains("uploaded"))
                    {
                        parameter[2] = "Y";
                    }
                    else
                    {
                        parameter[2] = "N";
                    }
                    CCRTable.Rows.Add(parameter);
                }
            }
            CCRTable.DefaultView.Sort = HiddenFieldSortExpression.Value + " " + HiddenFieldSortOrder.Value;
            if (CCRTable.Rows.Count > 0)
            {
                LabelNoRecords.Visible = false;
                GridViewCCRDocuments.DataSource = CCRTable.DefaultView;
                GridViewCCRDocuments.DataBind();
            }
            else
            {
                LabelNoRecords.Text = "No CCR records found";
                LabelNoRecords.Visible = true;
            }
        }
    }

    public void Button1_Click(object o, EventArgs e)
    {
        LinkButton Button1 = (LinkButton)o;
        GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
        LinkButton documentName = (LinkButton)grdRow.Cells[1].FindControl("LabelDocumentName");
        Label uploaded = (Label)grdRow.Cells[2].FindControl("LabelUploadFlag");
        string filename = documentName.Text;
        string ccrfilePath = string.Empty;
        if (uploaded.Text.ToUpper().Equals("Y"))
            ccrfilePath = Path.Combine(ControlHelperClass.CCRFolderPath + @"\Uploaded", filename);
        else
            ccrfilePath = Path.Combine(ControlHelperClass.CCRFolderPath, filename);
        if (!string.IsNullOrEmpty(ccrfilePath))
        {
            if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["CCRXslFilePath"].ToString()))
            {
                MultiViewCCR.ActiveViewIndex = 0;
                CCRXml.TransformSource = ConfigurationManager.AppSettings["CCRXslFilePath"].ToString();
                CCRXml.DocumentSource = ccrfilePath;
            }
            else
            {
                MultiViewCCR.ActiveViewIndex = 1;
                using (StreamReader ccrStream = new StreamReader(ccrfilePath, true))
                {
                    DivCCRXml.InnerHtml = ControlHelperClass.HandleJunkChar(ccrStream.ReadToEnd());
                }
            }
        }
    }
    protected void GridViewCCRDocuments_Sorting(object sender, GridViewSortEventArgs e)
    {
        HiddenFieldSortExpression.Value = e.SortExpression;
        if (HiddenFieldSortOrder.Value != ("ASC"))
        {
            HiddenFieldSortOrder.Value = "ASC";
        }
        else
        {
            HiddenFieldSortOrder.Value = "DESC";
        }
        BindGridView();
    }

    protected void GridViewCCRDocuments_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewCCRDocuments.PageIndex = e.NewPageIndex;
        BindGridView();
    }
}
